package com.eastseeway.radioproject.utils;

import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileTransferManager {
    private final List<String> fileQueue = new ArrayList<>();
    private int currentIndex = 0;
    private int currentOffset = 0;
    private boolean sendingCRC = false;
    private final Map<String, FileMeta> fileMetaMap = new HashMap<>();

    // 文件元数据内部类
    private static class FileMeta {
        final long totalSize;
        long transferred;

        FileMeta(long totalSize) {
            this.totalSize = totalSize;
        }
    }

    public void addFiles(List<String> files) {
        Log.e("12121",""+fileMetaMap.size() + fileQueue.size());
        for (String path : files) {
            File file = new File(path);
            fileMetaMap.put(path, new FileMeta(file.length()));
        }
        fileQueue.addAll(files);
    }

    public boolean hasNextFile() {
        return currentIndex < fileQueue.size();
    }

    public String getCurrentFilePath() {
        return (currentIndex < fileQueue.size()) ? fileQueue.get(currentIndex) : null;
    }

    public void updateProgress(int chunkSize) {
        String path = getCurrentFilePath();
        if (path != null) {
            FileMeta meta = fileMetaMap.get(path);
            meta.transferred = Math.min(meta.transferred + chunkSize, meta.totalSize);
        }
    }

    public void markFileComplete() {
        String path = getCurrentFilePath();
        if (path != null) {
            FileMeta meta = fileMetaMap.get(path);
            meta.transferred = meta.totalSize;
        }
    }

    public List<TransferProgress> getAllProgress() {
        List<TransferProgress> progresses = new ArrayList<>();
        for (int i = 0; i < fileQueue.size(); i++) {
            String path = fileQueue.get(i);
            FileMeta meta = fileMetaMap.get(path);
            int percent = (int) (meta.transferred * 100 / meta.totalSize);
            progresses.add(new TransferProgress(
                    new File(path).getName(),
                    percent,
                    i == currentIndex
            ));
        }
        return progresses;
    }

    public void moveToNextFile() {
        currentIndex++;
        currentOffset = 0;
        sendingCRC = false;
    }

    public List<String> getFileQueue() {
        return fileQueue;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public int getCurrentOffset() {
        return currentOffset;
    }

    public void setCurrentOffset(int currentOffset) {
        this.currentOffset = currentOffset;
    }

    public boolean isSendingCRC() {
        return sendingCRC;
    }

    public void setSendingCRC(boolean sendingCRC) {
        this.sendingCRC = sendingCRC;
    }

    public Map<String, FileMeta> getFileMetaMap() {
        return fileMetaMap;
    }
}
